﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NetCoreMvcTest.Middleware
{
    public class MyAshxMiddleware
    {
        private readonly RequestDelegate _next;

        public MyAshxMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            // Do something with context near the beginning of request processing.

            var path = context?.Request?.Path;

            if (path!=null && path.HasValue && path.ToString().EndsWith(".ashx", StringComparison.OrdinalIgnoreCase))
            {
                // 一般处理程序直接返回
                await context.Response.WriteAsync(path);
                return;
            }
            //不是ashx 一般处理程序： 下一步继续
            await _next.Invoke(context);
            // Clean up.
        }
    }
    public static class MyAshxMiddlewareExtensions
    {
        public static IApplicationBuilder UseMyAshxMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<MyAshxMiddleware>();
        }
    }
}
